/**
 * Created by meiguizhinian on 2018/7/6
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
unblockme.GateScene = LLScene.extend({
	res : [
		"res/game/UnblockMe/NETDATA/ANIMATION/Gate.actor",

		"res/game/UnblockMe/vfs/bg_game.png",
		"res/game/UnblockMe/vfs/back.png",
		"res/game/UnblockMe/vfs/start_box.png",
		"res/game/UnblockMe/vfs/star_gold.png",
		"res/game/UnblockMe/vfs/gate.png",
		"res/game/UnblockMe/vfs/medal.png",
		//music
		"res/game/UnblockMe/audio/effect/return.mp3",
		//data
		"res/game/UnblockMe/data/gate.csv",
	],
	ctor: function (form, space=null) {
		this._super(form, space);

		let button_back = this._formNode.getChildByName("bt1");
		button_back.setTouchEndCallback(function() {
			cc.LoaderScene.preload(unblockme.HomeScene.prototype.res, function () {
				let scene = unblockme.HomeScene.create("Home");
				cc.director.runScene(scene);
			}, this);
		});

		this._gateCount = unblockme.Recorder.getGateSize();
		this._pageMax = Math.ceil(this._gateCount/20.0);
		let starsMax = this._gateCount*3;
		let starsGain = unblockme.Recorder.sumGateStarCount();
		let label = this._formNode.getChildByName("tb4", ivy.UILabelTTF);
		label.setString(cc.formatStr("%d/%d", starsGain, starsMax));

		this.select(Math.floor(unblockme.Recorder.getProgressMax()/20)); //每页20关

		let listener = cc.EventListener.create({
			event : cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches : true,
			onTouchBegan : function () {
				return true;
			},
			onTouchEnded : function (touch) {
				let vec = cc.pSub(touch.getLocation() , touch.getStartLocation());
				if (vec.x<-100) {
					if (this._curPage<this._pageMax-1) {
						this.select(this._curPage+1);
					}
				} else if (vec.x>100) {
					if (this._curPage>0) {
						this.select(this._curPage-1);
					}
				}
			}.bind(this),
		});
		cc.eventManager.addListener(listener, this);
		return true;
	},

	select : function(page) {
		if (!this._curPanel) {//新建
			this._curPage = page;
			this._curPanel = this._formNode.getChildByName("or5", ivy.UIFlowLayoutPanel);
			this._pushGateToPanel(this._curPanel, this._curPage);
		} else if (page===this._curPage) {
			return;
		} else {
			if (!this._lastPanel) {//如果不存在上一个，新建
				this._lastPanel = this._curPanel;
				this._lastPage = this._curPage;

				this._curPage = page;
				this._curPanel = this._formNode.getChildByName("or10", ivy.UIFlowLayoutPanel);
				this._pushGateToPanel(this._curPanel, this._curPage);
			} else {
				let tmp;
				tmp = this._lastPage; this._lastPage = this._curPage; this._curPage = tmp;
				tmp = this._lastPanel; this._lastPanel = this._curPanel; this._curPanel = tmp;

				if (page!==this._curPage) {//页数不同，重建lastPanel所有内容
					this._curPage = page;
					this._curPanel.removeAll();
					this._pushGateToPanel(this._curPanel, this._curPage);
				}
			}
			//移出last，移入cur
			let center = this._lastPanel.getPosition();
			let lastPos = cc.p(center.x, center.y);
			let curPos = cc.p(center.x, center.y);

			if (this._lastPage>this._curPage) {//last 右出，cur 左入
				lastPos.x += 720;
				curPos.x -= 720;
			} else {//last左出，cur 右入
				lastPos.x -= 720;
				curPos.x += 720;
			}
			let act = cc.moveTo(0.2, lastPos);
			this._lastPanel.runAction(act);

			this._curPanel.setPosition(curPos);
			act = cc.moveTo(0.2, center);
			this._curPanel.runAction(act);
		}
		this._updateIndicate();
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////private
	_createGate : function(gate, progress, direction) {
		let element = ivy.UIManager.getInstance().createUIControlByName(this._formName, "bt2");

		let label = element.getChildByName("tb5", ivy.UILabelTTF);
		label.setString(gate+1);

		let stars = ["or6","or7","or8","or9"];//or9 // 奖章
		for (let i=0; i!==stars.length; ++i) {
			let star = element.getChildByName(stars[i]);
			star.setVisible(false);
		}

		if (gate<=progress) {
			if (gate<progress) {
				let starCount = unblockme.Recorder.getGateStarCount(gate);
				for (let i=0; i!==starCount; ++i) {
					let star = element.getChildByName(stars[i]);
					star.setVisible(true);
				}
			}
			//添加触摸
			element.setTouchEndCallback(function() {
				cc.LoaderScene.preload(unblockme.GameScene.prototype.res, function () {
					let scene = unblockme.GameScene.create("Game");
					scene.select(gate);
					cc.director.runScene(scene);
				}, this);
			});
		} else {
			element.setEnableUserTouch(false);
			element.setColor(cc.color(150,150,150));
		}
		return element;
	},
	_pushGateToPanel : function(panel, page) {
		let progress = unblockme.Recorder.getProgressMax();
		let gate_began = page*20;
		let gate_ended = gate_began+20;
		gate_ended = Math.min(gate_ended, this._gateCount);

		panel.pauseAutoLayout();
		for (let gate=gate_began; gate!==gate_ended; ++gate) {
			let element = this._createGate(gate, progress, 1);
			panel.pushBack(element);
		}
		panel.resumeAutoLayout();
	},

	_updateIndicate : function() {
		let label = this._formNode.getChildByName("tb7", ivy.UILabelTTF);
		label.setString(cc.formatStr("%d/%d",this._curPage+1, this._pageMax));
	},

	_pageMax : 0,
	_gateCount : 0,

	_curPage : 0,
	_curPanel : null,//cc::UIFlowLayoutPanel*
	_lastPage : 0,
	_lastPanel : null,//cc::UIFlowLayoutPanel*
});

unblockme.GateScene.create = function (form, space=null) {
	return new unblockme.GateScene(form, space);
};
